package com.ruoyi.annuity.service.impl;

import com.ruoyi.annuity.domain.TbAnnuityRecord;
import com.ruoyi.annuity.domain.TbLessee;
import com.ruoyi.annuity.mapper.TbAnnuityRecordMapper;
import com.ruoyi.annuity.service.ITbAnnuityRecordService;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 年费信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-24
 */
@Service
public class TbAnnuityRecordServiceImpl implements ITbAnnuityRecordService 
{
    @Autowired
    private TbAnnuityRecordMapper tbAnnuityRecordMapper;

    /**
     * 查询年费信息
     * 
     * @param annuityRecordId 年费信息主键
     * @return 年费信息
     */
    @Override
    public TbAnnuityRecord selectTbAnnuityRecordByAnnuityRecordId(Long annuityRecordId)
    {
        return tbAnnuityRecordMapper.selectTbAnnuityRecordByAnnuityRecordId(annuityRecordId);
    }

    /**
     * 查询年费信息列表
     * 
     * @param tbAnnuityRecord 年费信息
     * @return 年费信息
     */
    @Override
    public List<TbAnnuityRecord> selectTbAnnuityRecordList(TbAnnuityRecord tbAnnuityRecord)
    {
        return tbAnnuityRecordMapper.selectTbAnnuityRecordList(tbAnnuityRecord);
    }

    /**
     * 新增年费信息
     * 
     * @param tbAnnuityRecord 年费信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbAnnuityRecord(TbAnnuityRecord tbAnnuityRecord)
    {
        int rows = tbAnnuityRecordMapper.insertTbAnnuityRecord(tbAnnuityRecord);
        insertTbLessee(tbAnnuityRecord);
        return rows;
    }

    /**
     * 修改年费信息
     * 
     * @param tbAnnuityRecord 年费信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbAnnuityRecord(TbAnnuityRecord tbAnnuityRecord)
    {
        tbAnnuityRecordMapper.deleteTbLesseeByLesseeId(tbAnnuityRecord.getAnnuityRecordId());
        insertTbLessee(tbAnnuityRecord);
        return tbAnnuityRecordMapper.updateTbAnnuityRecord(tbAnnuityRecord);
    }

    /**
     * 批量删除年费信息
     * 
     * @param annuityRecordIds 需要删除的年费信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbAnnuityRecordByAnnuityRecordIds(Long[] annuityRecordIds)
    {
        tbAnnuityRecordMapper.deleteTbLesseeByLesseeIds(annuityRecordIds);
        return tbAnnuityRecordMapper.deleteTbAnnuityRecordByAnnuityRecordIds(annuityRecordIds);
    }

    /**
     * 删除年费信息信息
     * 
     * @param annuityRecordId 年费信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbAnnuityRecordByAnnuityRecordId(Long annuityRecordId)
    {
        tbAnnuityRecordMapper.deleteTbLesseeByLesseeId(annuityRecordId);
        return tbAnnuityRecordMapper.deleteTbAnnuityRecordByAnnuityRecordId(annuityRecordId);
    }

    /**
     * 新增租户信息
     * 
     * @param tbAnnuityRecord 年费信息对象
     */
    public void insertTbLessee(TbAnnuityRecord tbAnnuityRecord)
    {
        List<TbLessee> tbLesseeList = tbAnnuityRecord.getTbLesseeList();
        Long annuityRecordId = tbAnnuityRecord.getAnnuityRecordId();
        if (StringUtils.isNotNull(tbLesseeList))
        {
            List<TbLessee> list = new ArrayList<TbLessee>();
            for (TbLessee tbLessee : tbLesseeList)
            {
                tbLessee.setLesseeId(annuityRecordId);
                list.add(tbLessee);
            }
            if (list.size() > 0)
            {
                tbAnnuityRecordMapper.batchTbLessee(list);
            }
        }
    }
}
